package com.neuedu.javase.array;

/**
 * @author :金山老师
 * project: MyJavaSE
 * 描述 ： 数组
 * @date : 2025/10/22 11:04 星期三
 */
public class MyArray {


    public static void main(String[] args) {

        // int  age1 = 20;
        // int  age2 = 20;
        // int  age3 = 20;
        // int  age4 = 20;
        // int  age5 = 20;


        //定义5个 连续  int 类型数据 内存
        //数组的定义  new 的过程申请内存
        //int [] age  =   new int[5];
        // int[]  age  =   new int[5];
        int  age[]  =   new int[10];

        //获取数组的长度  xxxx.length属性
        System.out.println("========================数组的属性: =============================");
        System.out.println("age.length = " + age.length);


        // 访问元素  通过 下标的形式访问元素   下标 index   :   0     length - 1
        System.out.println("age[0] = " + age[0]);     //  int 数组默认值是0
        System.out.println("age[1] = " + age[1]);     //  int 数组默认值是0
        System.out.println("age[2] = " + age[2]);     //  int 数组默认值是0

        // 遍历:   利用length属性  挨个打印数组中的每个元素
        System.out.println("========================数组的遍历: =============================");
        for (int i = 0; i < age.length; i++) {
            System.out.println("使用for 循环遍历  age["+i+"] = " + age[i]);     //  int 数组默认值是0
        }


        /**
         *  数组的默认值
         *   byte short int long   默认 0
         *   float  double    默认 0.0
         *   char            默认    \u0000 不可见的空白符号
         *   boolean        默认 false
         */

        byte btArray[] = new byte[2];
        short stArray[] = new short[2];
        int intArray[] = new int[2];
        long lgArray[] = new long[2];
        float ftArray[] = new float[2];
        double dblArray[] = new double[2];

        char  chArray[] = new char[2];
        boolean  boolArray[] = new boolean[2];

        System.out.println("========================数组的默认值: =============================");
        System.out.println("btArray[0] = " + btArray[0]);       // 0
        System.out.println("stArray[0] = " + stArray[0]);        // 0
        System.out.println("intArray[0] = " + intArray[0]);      // 0
        System.out.println("lgArray[0] = " + lgArray[0]);       // 0
        System.out.println("ftArray[0] = " + ftArray[0]);       //0.0
        System.out.println("dblArray[0] = " + dblArray[0]);     //0.0
        System.out.println("chArray[0] = " + chArray[0]);       //  \u0000
        System.out.println("boolArray[0] = " + boolArray[0]);  // false




        //静态初始化
        // int ages[] = { 20, 30  , 25};
        // int ages[] = new int[]{ 20, 30  , 25};
        int ages[] = new int[]{ 20, 30  , 25};



        //数组的访问

        //修改数组中的元素
        ages[0] = 100;


        System.out.println("==================访问数组==========================");
        System.out.println("ages[0] = " + ages[0]); //100
        System.out.println("ages[1] = " + ages[1]); //30
        System.out.println("ages[2] = " + ages[2]); //25





        //数组的异常
        System.out.println("========================数组的异常: =============================");

        /**
         * 长度为：3  访问  5   超过范围
         * Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 5 out of bounds for length 3
         * 	at com.neuedu.javase.array.MyArray.main(MyArray.java:96)
         */
        // ages[5] = 100;


        // String str = "张三";

        /**
         * Exception in thread "main" java.lang.NullPointerException: Cannot read the array length because "str" is null
         * 	at com.neuedu.javase.array.MyArray.main(MyArray.java:108)
         */
        //定义数组，没有new
//        String str[] = null ;
//        System.out.println(str.length);

        int testArray[] = new int[]{10,20,30};
//       0        2
        testArray[0] = 100;
        System.out.println("testArray[4] = " + testArray[4]);


    }


}
